import { createSlice } from "@reduxjs/toolkit";
import { Toast } from "antd-mobile";
//  delPer(state, { payload }) {
// //       //过滤掉要删除的乘车人
// //       state.tickPersonList = state.tickPersonList.filter(
// //         (item) => item.id !== payload.id
// //       );
// //     },
export const TickSlice = createSlice({
  name: "TickSlice",
  initialState: {
    TickListData: [
      {
        id: 1,
        code: "30273948920394982173348",
        name: "无限",
        status: false,
      },
      {
        id: 2,
        code: "98236472837487283748783",
        name: "梦红尘",
        status: false,
      },
      {
        id: 3,
        code: "37238290829038837190127",
        name: "那年仲夏",
        status: false,
      },
      {
        id: 4,
        code: "83879289289472910287827",
        name: "三千尺",
        status: false,
      },
    ],
  },
  // Object.assign(item, payload);
  reducers: {
    addTick(state, { payload }) {
      const item = state.TickListData.find((item) => item.id === payload.id);
      if (!item) {
        state.TickListData.push(payload);
        Toast.show({
          icon: "success",
          content: "添加成功",
        });
      } else {
        Toast.show({
          icon: "fail",
          content: "此人已存在",
        });
      }
    },
    delTick(state, { payload }) {
      state.TickListData=state.TickListData.filter(
        (item)=>item.id!==payload.id
      )
    },  

    editTick(state,{payload}){
      const item=state.TickListData.find((item)=>item.id===payload.id)
      if(item){
        Object.assign(item,payload)
        Toast.show({
          icon:"success",
          content:"添加成功"
        })
      }
    }
  },
});

// Action creators are generated for each case reducer function
export const { addTick,delTick ,editTick} = TickSlice.actions;

export default TickSlice.reducer;
